/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.dao.DrugClassDictDao;
import com.jims.master.modules.drug.entity.*;
import com.jims.master.modules.drug.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 药理信息浏览器Controller
 * @author DT
 * @version 2017-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugInfoBrowser")
public class DrugInfoBrowserController extends BaseController {

	@Autowired
	private DrugNameDictService drugNameDictService;
	@Autowired
	private DrugStockService drugStockService;
	@Autowired
	private DrugDetailsService drugDetailsService;
	@Autowired
	private DrugInfoService drugInfoService;
	@Autowired
	private DrugClassDictDao drugClassDictDao;
	@ModelAttribute
	public DrugNameDict get(@RequestParam(required=false) String id) {
		DrugNameDict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugNameDictService.get(id);
		}
		if (entity == null){
			entity = new DrugNameDict();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String index(DrugInfo drugInfo,HttpServletRequest request, HttpServletResponse response, Model model) {
		if(drugInfo.getDrugDict()!=null){
            DrugDict drugDict=new DrugDict();
            if(drugInfo.getDrugDict().getDrugClassID()!=null && !"".equals(drugInfo.getDrugDict().getDrugClassID()) ){
                DrugClassDict drugClassDict = drugClassDictDao.get(drugInfo.getDrugDict().getDrugClassID());
                drugDict.setDrugCode(drugClassDict.getClassCode());
            }
			drugDict.setDrugName(drugInfo.getDrugDict().getDrugName());
			drugInfo.setDrugDict(drugDict);
		}
		List<DrugNameDict> list=drugNameDictService.getDrugNameList(new DrugNameDict());
		model.addAttribute("list",list);
		Page<DrugInfo> page=drugInfoService.findDrugInfoList(new Page<DrugInfo>(request,response),drugInfo);
		model.addAttribute("page",page);
		model.addAttribute("drugInfo",drugInfo);
		return "modules/drug/drugInfoBrowser";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"dialog", ""})
	public String dialog(DrugDetails drugDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
		drugDetails=drugDetailsService.findBydrugInfoId(drugDetails);
		model.addAttribute("drugDetails", drugDetails);
		return "modules/drug/drugDetailsInfo";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"stockDetails", ""})
	public String stockDetails(DrugStock drugStock, HttpServletRequest request, HttpServletResponse response, Model model) {
//		drugStock = drugStockService.findDrugByInfoId(drugStock);
		List<DrugStock> list=drugStockService.findDrugByInfoId(drugStock);
		model.addAttribute("list", list);
		return "modules/drug/drugStockDetails";
	}

}